package com.ruifu.bom.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruifu.bom.feign.DocDocumentFeign;
import com.ruifu.bom.mapper.MaterielDocumentMapper;
import com.ruifu.bom.service.MaterielDocumentService;
import com.ruifu.bom.vo.RelevanceDocumentVO;
import com.ruifu.bom.vo.docfeignvo.DmDocVerVO;
import com.ruifu.common.po.MaterielDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lcq
 * @create 2020/1/2
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MaterielDocumentImpl extends ServiceImpl<MaterielDocumentMapper, MaterielDocument> implements MaterielDocumentService {

    @Autowired(required = false)
    MaterielDocumentMapper materielDocumentMapper;

    @Autowired
    DocDocumentFeign docDocumentFeign;

    /**
     * 批量删除关联文档
     *
     * @param mdIds
     * @return
     */
    @Override
    public R deleteBatch(List<Long> mdIds) {
        if (CollectionUtil.isNotEmpty(mdIds)) {
            materielDocumentMapper.deleteBatchIds(mdIds);
        }
        return R.ok("删除成功");
    }

    /**
     * 批量新增关联文档
     *
     * @param mdVerId
     * @param dcVerIds
     * @return
     */
    @Override
    public R addBatch(Long mdVerId, List<Long> dcVerIds) {
        if (CollectionUtil.isNotEmpty(dcVerIds) && null != mdVerId) {
            //去重操作
            dcVerIds = dcVerIds.stream().distinct().collect(Collectors.toList());

            int count;
            for (Long dcVerId : dcVerIds) {
                count = materielDocumentMapper.selectCount(Wrappers.<MaterielDocument>lambdaQuery().
                        eq(MaterielDocument::getMdDocVersionsId, dcVerId).
                        eq(MaterielDocument::getMdId, mdVerId));

                if (count == 0) {
                    //数据不存在则插入
                    MaterielDocument mDocument = new MaterielDocument();
                    mDocument.setMdVersionsId(mdVerId);
                    mDocument.setMdDocVersionsId(dcVerId);
//                    mDocument.setMdAssociationType(false);    //是否为主控文档
//                    mDocument.setMdVersionLock(false);    //是否锁定当前文档版本

                    materielDocumentMapper.insert(mDocument);
                }
            }
        }

        return null;
    }

    /**
     * 查看图档列表
     *
     * @param mdVersionsId
     * @return
     */
    @Override
    public List<RelevanceDocumentVO> findByMdVersionsId(Long mdVersionsId) {
        //新建List集合
        List<RelevanceDocumentVO> relevanceDocumentList = new ArrayList<>();

        //根据物料版本id查询物料文档集合
        List<MaterielDocument> materielDocuments = materielDocumentMapper.selectList(Wrappers.<MaterielDocument>lambdaQuery()
                .eq(MaterielDocument::getMdVersionsId, mdVersionsId));

        materielDocuments.forEach(materielDocument -> {
            //feign远程调用查询文档
            DmDocVerVO dmDocVerVO = docDocumentFeign.findById(materielDocument.getMdDocVersionsId());

            if (null != dmDocVerVO) {
                //创建vo
                RelevanceDocumentVO relevanceDocumentVO = new RelevanceDocumentVO();

                //是否为主控文档
                relevanceDocumentVO.setIsMasterControl(materielDocument.getMdAssociationType());
                //物料文档id
                relevanceDocumentVO.setDocumentId(materielDocument.getMdId());

                //文档外部版本号
                relevanceDocumentVO.setVersionsNumber(dmDocVerVO.getDvVerLabel());
                //数据状态  0-编辑中 3-流程中 1-已发布 2-已作废
                relevanceDocumentVO.setDocumentStatus(dmDocVerVO.getDvCtrStatus());
                //文档版本id
                relevanceDocumentVO.setDocumentDvId(dmDocVerVO.getDvDvId());

                //文档编号
                relevanceDocumentVO.setDocumentCode(dmDocVerVO.getDmDocMaster().getDmDocCode());
                //文档名称
                relevanceDocumentVO.setDocumentName(dmDocVerVO.getDmDocMaster().getDmDocName());
                //文档类型名称
                relevanceDocumentVO.setDocumentTypeName(dmDocVerVO.getDmDocMaster().getDmDocc().getDcDoccName());
                //文档类别
                relevanceDocumentVO.setDocumentType(dmDocVerVO.getDmDocMaster().getDmDocc().getDcType());

                //文档文件后缀(格式)
                relevanceDocumentVO.setDocumentFormat(dmDocVerVO.getDmFile().getFiFileName()
                        .substring(dmDocVerVO.getDmFile().getFiFileName().lastIndexOf(".") + 1));
                //文档-文件id
                relevanceDocumentVO.setDocumentFileId(dmDocVerVO.getDmFile().getFiFileId());

                relevanceDocumentList.add(relevanceDocumentVO);
            }
        });
        return relevanceDocumentList;
    }
}
